//
//  SilverLightObject.m
//  TowerDefence
//
//  Created by k3 on 6/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SilverLightObject.h"
#import "Enemy.h"
#import "iCoreGUIController.h"

@implementation SilverLightObject

-(id)initSilverLightObjectWith:(GameLayer *)_gameLayer {
    self = [super initGameObjectWith:_gameLayer];
    lightDamage = 50;
    spriteBody = [[CCSprite alloc] init];
    return self;
}

-(void)loadSilverLight {
    CCAnimation *animation = [handler.mainGUIController loadAnimationFromCacheByName:@"silver_light_effect" withCount:9 delay:0.025];
    [spriteBody setRotation:20.0f];
    //[spriteBody setScale:0.3];
    [spriteBody setAnchorPoint:ccp(0.5, 0)];
    
    CCCallFunc *function = [CCCallFunc actionWithTarget:self selector:@selector(burnTarget)];
    
    CGPoint targetPoint = [((Enemy *)target) getPositionAfterTime:0.75];
    targetPoint = ccp(targetPoint.x, targetPoint.y + target.spriteBody.contentSize.height/2);
    [spriteBody runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.75f position:targetPoint],[CCAnimate actionWithAnimation:animation],function, nil]];
}

-(void)dropToTarget:(GameObject *)_target {
    target = _target;
    [self loadSilverLight];
}

-(void)burnTarget {
    /*
    if(!handler.mainGUIController.isMuteSound) {
        [audioPlayer playWithFileName:AUDIO_SILVER_LIGHT loop:NO];
        [audioPlayer setVolume:1.25f];
    }*/
    
    [self.spriteBody stopAllActions];
    
    if([handler.listGameObject containsObject:target]){        
        ((Enemy *) target).HP -= lightDamage;
        [((Enemy *) target) updateBloodMeter];
        [((Enemy *) target) animationHit];
        /*
        if(((Enemy *) target).HP <= 0) {
            [((Enemy *) target) animationDie];
        }*/
    }
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self.spriteBody removeFromParentAndCleanup:YES];
    [handler removeGameObject:self];
}


-(void)moveAnimationWithFrameName:(NSString *)_frameName frameCount:(int)_frameCount {
    NSMutableArray *arrayFrame = [self loadAnimationWithNumberOfFrames:_frameCount withFrameName:_frameName];
    CCAnimation *animation = [CCAnimation animationWithFrames:arrayFrame delay:0.075];
    
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
}

-(void)dealloc {
    NSLog(@"silver light dealloc");
    [handler.mainGUIController removeAnimationInFrameCache:@"silver_light_effect"];
    [super dealloc];
}

@end
